package study.ssm.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.ssm.entity.Cart;
import study.ssm.mapper.CartMapper;
import study.ssm.mapper.ProductMapper;
import study.ssm.service.CartService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class CartServiceImpl implements CartService {

    private final CartMapper cartMapper;
    private final ProductMapper productMapper;


    @Override
    public Integer create(Cart cart) {
        return cartMapper.insert(cart);
    }

    @Override
    public Integer removeById(Integer id) {
        return cartMapper.deleteById(id);
    }

    @Override
    public Integer update(Cart cart) {
        return cartMapper.updateById(cart);
    }

    @Override
    public List<Cart> list(Cart cart) {
        List<Cart> carts = cartMapper.selectList(cart);
        for (Cart item : carts) {
            item.setProduct(productMapper.selectById(item.getProductId()));
        }
        return carts;
    }

    @Override
    public Cart getById(Integer id) {
        Cart cart = cartMapper.selectById(id);
        cart.setProduct(productMapper.selectById(cart.getProductId()));
        return cart;
    }
}
